package me.kafeitu.activiti.chapter10;

import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.Deployment;
import org.activiti.spring.impl.test.SpringActivitiTestCase;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 购买办公用品流程测试
 *
 * @author henryyan
 */
@ContextConfiguration("classpath:applicationContext-test-chapter10.xml")
public class MultiInstanceForSubprocessTestC extends SpringActivitiTestCase {

    @Test
    @Deployment(resources = {"diagrams/chapter10/multiinstance-for-subprocessC.bpmn"})
    public void testOne() throws Exception {

        identityService.setAuthenticatedUserId("henryyan");

        Map<String, Object> variables = new HashMap<String, Object>();
        List<String> users = Arrays.asList("user1", "user2", "user3");
        variables.put("users", users);
        variables.put("shiLeaderAssis","bill");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("multiinstance-for-subprocess", variables);

        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskCandidateOrAssigned("bill").singleResult();
        // taskService.claim(task.getId(), "bill");
        // set local var
        //taskService.setVariable(task.getId(), "deptLeader", "test1");
        System.out.println("task = " + task);

        for (String user : users) {
            long count = taskService.createTaskQuery().taskAssignee(user).count();
            assertEquals(1, count);
            Task subTask = taskService.createTaskQuery().taskAssignee(user).singleResult();
            taskService.complete(subTask.getId());
        }
        
        //Task endTask = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskCandidateOrAssigned("bill").singleResult();
        List<Task> listTask = taskService.createTaskQuery().processInstanceId(processInstance.getId()).
                taskCandidateOrAssigned("bill").list();
        
        int rejectCount = 1;
        for (Task curTask:listTask) {
            System.out.println("curTask = " + curTask);
            if (curTask.getTaskDefinitionKey().equals("shiAudit") && rejectCount-- > 0) {
                Map<String, Object> variablesShiLeaderAssis = new HashMap<String, Object>();
                variablesShiLeaderAssis.put("shiLeaderAssisPass",false);
                taskService.complete(curTask.getId(), variablesShiLeaderAssis);
            } 
        }
        // set local var
        //taskService.setVariable(task.getId(), "deptLeader", "endtest");
        //taskService.setVariableLocal(endTask.getId(), "deptLeader", "endtest2");
        // taskService.claim(endTask.getId(), "bill");
        
        // complete 市总
        Map<String, Object> variablesShiLeader = new HashMap<String, Object>();
        variablesShiLeader.put("shiLeader","dingz");
        taskService.complete(task.getId(), variablesShiLeader);
        
        // shiApprove
        Task shiApproveTask = taskService.createTaskQuery().processInstanceId(processInstance.getId())
                .taskCandidateOrAssigned("dingz").singleResult();
        taskService.complete(shiApproveTask.getId());

        System.out.println("Ended");
    }
    

    //@Test
    @Deployment(resources = {"diagrams/chapter10/multiinstance-for-subprocessC.bpmn"})
    public void testTwo() throws Exception {

        identityService.setAuthenticatedUserId("henryyan");

        Map<String, Object> variables = new HashMap<String, Object>();
        List<String> users = Arrays.asList("user1", "user2", "user3");
        variables.put("users", users);
        variables.put("shiLeaderAssis","bill");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("multiinstance-for-subprocess", variables);

        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskCandidateOrAssigned("bill").singleResult();
        // taskService.claim(task.getId(), "bill");
        // set local var
        //taskService.setVariable(task.getId(), "deptLeader", "test1");
        System.out.println("task = " + task);

        for (String user : users) {
            long count = taskService.createTaskQuery().taskAssignee(user).count();
            assertEquals(1, count);
            Task subTask = taskService.createTaskQuery().taskAssignee(user).singleResult();
            taskService.complete(subTask.getId());
        }
        
        //Task endTask = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskCandidateOrAssigned("bill").singleResult();
        List<Task> listTask = taskService.createTaskQuery().processInstanceId(processInstance.getId()).
                taskCandidateOrAssigned("bill").list();
        
        int rejectCount = 1;
        for (Task curTask:listTask) {
            System.out.println("curTask = " + curTask);
            if (curTask.getTaskDefinitionKey().equals("shiAudit")) {
                Map<String, Object> variablesShiLeaderAssis = new HashMap<String, Object>();
                variablesShiLeaderAssis.put("shiLeaderAssisPass",true);
                taskService.complete(curTask.getId(), variablesShiLeaderAssis);
            } 
        }
        // set local var
        //taskService.setVariable(task.getId(), "deptLeader", "endtest");
        //taskService.setVariableLocal(endTask.getId(), "deptLeader", "endtest2");
        // taskService.claim(endTask.getId(), "bill");
        
        // complete 市总
        Map<String, Object> variablesShiLeader = new HashMap<String, Object>();
        variablesShiLeader.put("shiLeader","dingz");
        taskService.complete(task.getId(), variablesShiLeader);
        
        // shiApprove
        Task shiApproveTask = taskService.createTaskQuery().processInstanceId(processInstance.getId())
                .taskCandidateOrAssigned("dingz").singleResult();
        taskService.complete(shiApproveTask.getId());

        System.out.println("Ended");
    }
}
